home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter6 / dscrparm.c next >
C/C++ Source or Header  |  1996-04-27  |  13KB  |  350 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "dscrparm.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLDescribeParam()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25. VOID BuildOutputStr( LPSTR pszLBStr, SWORD fSqlType, UDWORD cbColDef, 
  26.                                      SWORD ibScale,  SWORD  fNullable );
  27.  
  28.  
  29. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                       LPTSTR lpCmdLine, int nCmdShow)
  31. {
  32.    MSG      msg;
  33.    HWND     hWnd; 
  34.    WNDCLASS wc;
  35.  
  36.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  37.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  38.    wc.cbClsExtra    = 0;                      
  39.    wc.cbWndExtra    = 0;                      
  40.    wc.hInstance     = hInstance;              
  41.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  42.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  44.    wc.lpszMenuName  = lpszAppName;              
  45.    wc.lpszClassName = lpszAppName;              
  46.  
  47.    if ( IS_WIN95 )
  48.    {
  49.       if ( !RegisterWin95( &wc ) )
  50.          return( FALSE );
  51.    }
  52.    else if ( !RegisterClass( &wc ) )
  53.       return( FALSE );
  54.  
  55.    hInst = hInstance; 
  56.  
  57.    hWnd = CreateWindow( lpszAppName, 
  58.                         lpszTitle,    
  59.                         WS_OVERLAPPEDWINDOW, 
  60.                         CW_USEDEFAULT, 0, 
  61.                         CW_USEDEFAULT, 0,  
  62.                         NULL,              
  63.                         NULL,              
  64.                         hInstance,         
  65.                         NULL               
  66.                       );
  67.  
  68.    if ( !hWnd ) 
  69.       return( FALSE );
  70.  
  71.    ShowWindow( hWnd, nCmdShow ); 
  72.    UpdateWindow( hWnd );         
  73.  
  74.    while( GetMessage( &msg, NULL, 0, 0) )   
  75.    {
  76.       TranslateMessage( &msg ); 
  77.       DispatchMessage( &msg );  
  78.    }
  79.  
  80.    return( msg.wParam ); 
  81. }
  82.  
  83.  
  84. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  85. {
  86.    WNDCLASSEX wcex;
  87.  
  88.    wcex.style         = lpwc->style;
  89.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  90.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  91.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  92.    wcex.hInstance     = lpwc->hInstance;
  93.    wcex.hIcon         = lpwc->hIcon;
  94.    wcex.hCursor       = lpwc->hCursor;
  95.    wcex.hbrBackground = lpwc->hbrBackground;
  96.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  97.    wcex.lpszClassName = lpwc->lpszClassName;
  98.  
  99.    // Added elements for Windows 95.
  100.    //...............................
  101.    wcex.cbSize = sizeof(WNDCLASSEX);
  102.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  103.                             IMAGE_ICON, 16, 16,
  104.                             LR_DEFAULTCOLOR );
  105.             
  106.    return RegisterClassEx( &wcex );
  107. }
  108.  
  109.  
  110. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  111. {
  112. static HENV  hEnv  = NULL;
  113. static HDBC  hDBC  = NULL;
  114. static HWND  hList = NULL;
  115.  
  116. static UCHAR szSqlStr[] = 
  117.    "Insert into department( dept_id, dept_name, dept_head_id ) "
  118.    "Values ( ?, ?, ? )";
  119. static UCHAR szLBStr[256];
  120.  
  121.    switch( uMsg )
  122.    {
  123.       case WM_CREATE :
  124.               {
  125.                  RETCODE retCode;
  126.  
  127.                  // Allocate Environment and Connection.
  128.                  //.....................................
  129.                  SQLAllocEnv( &hEnv );
  130.                  SQLAllocConnect( hEnv, &hDBC );
  131.  
  132.                  // Connect to the data source
  133.                  //...........................
  134.                  retCode = SQLConnect( hDBC,
  135.                              "Test Data Source", SQL_NTS,
  136.                              "DBA",              SQL_NTS,  
  137.                              "SQL",              SQL_NTS );
  138.  
  139.                  if ( retCode != SQL_SUCCESS )
  140.                  {
  141.                     SQLFreeConnect( hDBC );
  142.                     SQLFreeEnv( hEnv );
  143.                     return( -1 );
  144.                  }
  145.  
  146.                  if ( retCode == SQL_SUCCESS )
  147.                  {
  148.                     int nTabSize = 40;
  149.  
  150.                     hList = CreateWindow( "LISTBOX", "",    
  151.                                LBS_NOTIFY | LBS_USETABSTOPS |
  152.                                LBS_NOINTEGRALHEIGHT  | 
  153.                                WS_BORDER  | WS_CHILD | 
  154.                                WS_VISIBLE | WS_VSCROLL, 
  155.                                0, 0, 0, 0, 
  156.                                hWnd, (HMENU)101,
  157.                                hInst, NULL );
  158.                     
  159.                     SendMessage( hList, LB_SETTABSTOPS, 
  160.                                  1, (LPARAM)&nTabSize );
  161.                  }
  162.               }
  163.               break;
  164.  
  165.       case WM_SIZE :
  166.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  167.                                        HIWORD( lParam ), TRUE );
  168.               break; 
  169.               
  170.       case WM_COMMAND :
  171.               switch( LOWORD( wParam ) )
  172.               {
  173.                  case IDM_TEST :
  174.                         {
  175.                            HSTMT   hStmt;
  176.                            RETCODE retCode;
  177.  
  178.                            // Set connection to Manual commit mode
  179.                            // ..........................................
  180.                            SQLSetConnectOption( hDBC, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF );
  181.   
  182.                            // Allocate a statement handle for the query.
  183.                            // ..........................................
  184.                            SQLAllocStmt( hDBC, &hStmt );
  185.  
  186.                            // Call SQLPrepare() to compile the query.
  187.                            // .......................................
  188.                            retCode = SQLPrepare( hStmt, szSqlStr, SQL_NTS );
  189.                            
  190.                            if( retCode == SQL_SUCCESS ||
  191.                                retCode == SQL_SUCCESS_WITH_INFO )
  192.                            {
  193.                               UWORD  iParams = 1;
  194.                               SWORD  nParams;
  195.                               SWORD  fSqlType;
  196.                               UDWORD cbColDef; 
  197.                               SWORD  ibScale;
  198.                               SWORD  fNullable;
  199.  
  200.                               // Call SQLNumParams() to determine the
  201.                               // number of parameters in the statement.
  202.                               // ......................................
  203.                               SQLNumParams( hStmt, &nParams );
  204.  
  205.                               sprintf( szLBStr, "Number of parameters = %d",
  206.                                                  nParams );
  207.  
  208.                               SendMessage( hList, LB_ADDSTRING, 0, 
  209.                                                 (LPARAM)szLBStr );
  210.  
  211.                               // Call SQLDescribeParam() to determine 
  212.                               // parameter data for each parameter 
  213.                               // contained in the statement.
  214.                               // ....................................
  215.                               while( iParams <= nParams )
  216.                               {
  217.                                  retCode = SQLDescribeParam( hStmt, iParams, 
  218.                                     &fSqlType, &cbColDef, 
  219.                                     &ibScale,  &fNullable);
  220.  
  221.                                  if( retCode != SQL_SUCCESS &&
  222.                                      retCode != SQL_SUCCESS_WITH_INFO )
  223.                                     break;
  224.  
  225.                                  BuildOutputStr( szLBStr, fSqlType, 
  226.                                                           cbColDef, 
  227.                                                           ibScale,  
  228.                                                           fNullable );
  229.  
  230.                                  SendMessage( hList, LB_ADDSTRING, 0, 
  231.                                                    (LPARAM)szLBStr );
  232.  
  233.                                  iParams++;
  234.                               }
  235.                            }
  236.                            
  237.                            // Free the statement handle.
  238.                            // ..........................
  239.                            SQLFreeStmt( hStmt, SQL_DROP );
  240.                         }
  241.                         break;
  242.  
  243.                  case IDM_ABOUT :
  244.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  245.                         break;
  246.  
  247.                  case IDM_EXIT :
  248.                         DestroyWindow( hWnd );
  249.                         break;
  250.               }
  251.               break;
  252.       
  253.       case WM_DESTROY :
  254.               PostQuitMessage(0);
  255.  
  256.               // Disconnect Environment.
  257.               //........................
  258.               SQLDisconnect( hDBC );
  259.  
  260.               // Free Connection and Environment.
  261.               //.................................
  262.               SQLFreeConnect( hDBC );
  263.               SQLFreeEnv( hEnv );
  264.               break;
  265.  
  266.       default :
  267.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  268.    }
  269.  
  270.    return( 0L );
  271. }
  272.  
  273.  
  274. // Builds an output string based on the values 
  275. // returned from the SQLDescribeParam() function.
  276. // ..............................................
  277.  
  278. VOID BuildOutputStr( LPSTR pszLBStr, SWORD fSqlType, UDWORD cbColDef, 
  279.                                      SWORD ibScale,  SWORD  fNullable )
  280. {
  281.    char szTempStr[32];
  282.  
  283.    pszLBStr[0] = '\0';
  284.  
  285.    switch( fSqlType )
  286.    {
  287.       case SQL_BIGINT       : strcat( pszLBStr, "BIGINT"        ); break;
  288.       case SQL_BINARY       : strcat( pszLBStr, "BINARY"        ); break;
  289.       case SQL_BIT          : strcat( pszLBStr, "BIT"           ); break;
  290.       case SQL_CHAR         : strcat( pszLBStr, "CHAR"          ); break;
  291.       case SQL_DATE         : strcat( pszLBStr, "DATE"          ); break;
  292.       case SQL_DECIMAL      : strcat( pszLBStr, "DECIMAL"       ); break;
  293.       case SQL_DOUBLE       : strcat( pszLBStr, "DOUBLE"        ); break;
  294.       case SQL_FLOAT        : strcat( pszLBStr, "FLOAT"         ); break;
  295.       case SQL_INTEGER      : strcat( pszLBStr, "INTEGER"       ); break;
  296.       case SQL_LONGVARBINARY: strcat( pszLBStr, "LONGVARBINARY" ); break;
  297.       case SQL_LONGVARCHAR  : strcat( pszLBStr, "LONGVARCHAR"   ); break;
  298.       case SQL_NUMERIC      : strcat( pszLBStr, "NUMERIC"       ); break;
  299.       case SQL_REAL         : strcat( pszLBStr, "REAL"          ); break;
  300.       case SQL_SMALLINT     : strcat( pszLBStr, "SMALLINT"      ); break;
  301.       case SQL_TIME         : strcat( pszLBStr, "TIME"          ); break;
  302.       case SQL_TIMESTAMP    : strcat( pszLBStr, "TIMESTAMP"     ); break;
  303.       case SQL_TINYINT      : strcat( pszLBStr, "TINYINT"       ); break;
  304.       case SQL_VARBINARY    : strcat( pszLBStr, "VARBINARY"     ); break;
  305.       case SQL_VARCHAR      : strcat( pszLBStr, "VARCHAR"       ); break;
  306.       default:   strcat( pszLBStr, "Driver-specific data type." ); break;
  307.    }
  308.    strcat( pszLBStr, "\t" );
  309.  
  310.    sprintf(szTempStr, "%u", cbColDef );
  311.    strcat( pszLBStr, szTempStr );
  312.    strcat( pszLBStr, "\t" );
  313.  
  314.    sprintf(szTempStr, "%d", ibScale );
  315.    itoa( ibScale, szTempStr, 10 );
  316.    strcat( pszLBStr, szTempStr );
  317.    strcat( pszLBStr, "\t" );
  318.  
  319.    switch( fNullable )
  320.    {
  321.       case SQL_NO_NULLS:         strcat( pszLBStr, "No NULLs." );    break;
  322.       case SQL_NULLABLE:         strcat( pszLBStr, "Nullable." );    break;
  323.       case SQL_NULLABLE_UNKNOWN: strcat( pszLBStr, "NULL unknown." );break;
  324.    }
  325. }
  326.  
  327.  
  328. LRESULT CALLBACK About( HWND hDlg,           
  329.                         UINT message,        
  330.                         WPARAM wParam,       
  331.                         LPARAM lParam)
  332. {
  333.    switch (message) 
  334.    {
  335.        case WM_INITDIALOG: 
  336.                return (TRUE);
  337.  
  338.        case WM_COMMAND:                              
  339.                if (   LOWORD(wParam) == IDOK         
  340.                    || LOWORD(wParam) == IDCANCEL)    
  341.                {
  342.                        EndDialog(hDlg, TRUE);        
  343.                        return (TRUE);
  344.                }
  345.                break;
  346.    }
  347.  
  348.    return (FALSE); 
  349. }
  350.